package com.srit.forecast.dao.impl;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.srit.core.bean.Page;
import com.srit.forecast.bean.Broadcast;
import com.srit.forecast.bean.BroadcastArea;
import com.srit.forecast.dao.BroadcastDao;

@Service
public class BroadcastDaoImpl implements BroadcastDao {

	@Autowired(required = true)
	private JdbcTemplate jdbcTemplate;

	@Override
	public void save(Broadcast broadcast) {
		String sql = "insert into TC_BROADCAST(id,pubtime,reportInterval,todayMaxtemp,todayMintemp,warnType,warnContext,pubFlag,chief,collator) values(SEQ_TC_BROADCAST.nextval,?,?,?,?,?,?,?,?,?)";
		int id = jdbcTemplate.update(
				sql,
				new Object[] { broadcast.getPubTime(),
						broadcast.getReportInterval(),
						broadcast.getTodayMaxTemp(),
						broadcast.getTodayMinTemp(), broadcast.getWarnType(),
						broadcast.getWarnContext(), broadcast.isPubFlag(),
						broadcast.getChief(), broadcast.getCollator() });
		System.out.print(id);

	}

	@Override
	public void update(Broadcast bc) {
		// TODO Auto-generated method stub
		String sql = "update TC_BROADCAST set pubtime = ? , reportInterval = ? , todayMaxtemp = ? , todayMinTemp = ? , warnType = ?,warnContext = ?,pubFlag = ?,chief = ?,collator = ? where Id = ?";
		jdbcTemplate.update(
				sql,
				new Object[] { bc.getPubTime(), bc.getReportInterval(),
						bc.getTodayMaxTemp(), bc.getTodayMinTemp(),
						bc.getWarnType(), bc.getWarnContext(), bc.isPubFlag(),
						bc.getChief(), bc.getCollator(), bc.getId() });

	}

	@Override
	public Broadcast getById(Integer id) {
		// TODO Auto-generated method stub
		String sql = "select * from TC_BROADCAST where ID = ?";
		Broadcast broadcast = jdbcTemplate
				.queryForObject(sql, new Object[] { id },
						new BeanPropertyRowMapper(Broadcast.class));
		return broadcast;
	}

	// @Override
	// public void page(Page page) {
	// String sql = "select * from TC_BROADCAST where ID = ?";
	// // TODO Auto-generated method stub
	//
	// }

	@Override
	public List<Broadcast> findAll() {
		// TODO Auto-generated method stub
		String sql = "select * from TC_BROADCAST";

		List<Broadcast> bcs = new ArrayList<Broadcast>();

		List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql);

		for (Map row : rows) {
			Broadcast bc = new Broadcast();
			BigDecimal id = (BigDecimal) row.get("ID");
			bc.setId(id.intValue());
			// bc.setPubTime((Date)row.get("PUBTIME"));
			// bc.setTodayMaxTemp((String)row.get("CHIFE"));
			// bc.setTodayMinTemp((String)row.get("TODAYMINTEMP"));
			bcs.add(bc);
		}
		return bcs;
	}

	@Override
	public int getMaxId() {
		String sql = "select max(ID) as maxId from TC_BROADCAST";
		return jdbcTemplate.queryForInt(sql);
	}

	@Override
	public void saveArea(List<BroadcastArea> list) {
		// TODO Auto-generated method stub
		String sql = "insert into TC_BROADCASTAREA(id,broadcastId,areaId,weather,wind,todayMaxtemp,todayMintemp,remarker) values(SEQ_TC_BROADCASTAREA.nextval,?,?,?,?,?,?,?)";
		for (BroadcastArea bcArea : list) {
			jdbcTemplate.update(
					sql,
					new Object[] { bcArea.getBroadcastId(), bcArea.getAreaId(),
							bcArea.getWeather(), bcArea.getWind(),
							bcArea.getMaxTemp(), bcArea.getMinTemp(),
							bcArea.getRemarker() });
		}
	}

	@Override
	public void deleteAll() {
		// TODO Auto-generated method stub

	}

	@Override
	public void delete(Integer id) {
		// TODO Auto-generated method stub

	}

	@Override
	public void page(Page page) {
		// TODO Auto-generated method stub

	}

}

class BroadcastResultSetExtractor implements ResultSetExtractor {

	@Override
	public Object extractData(ResultSet rs) throws SQLException,
			DataAccessException {
		Broadcast bc = new Broadcast();
		// ID,PUBTIME,TODAYMAXTEMP,TODAYMINTEMP
		bc.setId(rs.getInt("ID"));
		bc.setPubTime(rs.getDate("PUBTIME"));
		bc.setReportInterval(rs.getString("REPORTINTERVAL"));
		bc.setTodayMaxTemp(rs.getString("TODAYMAXTEMP"));
		bc.setTodayMinTemp("TODAYMINTEMP");
		bc.setWarnType(rs.getString("WARNTYPE"));
		bc.setWarnContext(rs.getString("WARNCONTEXT"));
		bc.setChief(rs.getString("CHIEF"));
		bc.setCollator(rs.getString("COLLATOR"));
		bc.setPubFlag(rs.getBoolean("PUBFLAG"));
		return bc;
	}

}

class BroadcastRowMapper implements RowMapper {
	@Override
	public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
		BroadcastResultSetExtractor extractor = new BroadcastResultSetExtractor();
		return extractor.extractData(rs);
	}

}
